package com.chuanjiabao.wisdom.ui.activity

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.webkit.*
import com.chuanjiabao.wisdom.R
import com.chuanjiabao.wisdom.ui.BaseActivity
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import kotlinx.android.synthetic.main.activity_feedback.*
import java.io.File

class FeedbackActivity : BaseActivity() {

    private var mUploadCallbackAboveL: ValueCallback<Array<Uri>>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_feedback)
        initView()

        val url = "http://home.zzwisdom.com/index/feedback"
        llModuleWeb.loadUrl(url)
    }

    private fun initView() {
        llModuleWeb.apply {
            //支持javascript
            @Suppress("DEPRECATION")
            settings.javaScriptEnabled = true
            //设置可以支持缩放
            settings.setSupportZoom(true)
            //自适应屏幕
            settings.loadWithOverviewMode = true
            settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            isHorizontalScrollBarEnabled = false

            //设置出现缩放工具
            settings.builtInZoomControls = false
            settings.displayZoomControls = false

            //设置是否使用缓存
            settings.setAppCacheEnabled(true)
            settings.domStorageEnabled = true
            setBackgroundColor(0)
            // addJavascriptInterface(this@FeedbackActivity, "android")

            webChromeClient = object : WebChromeClient() {
                override fun onShowFileChooser(
                        webView: WebView,
                        filePathCallback: ValueCallback<Array<Uri>>?,
                        fileChooserParams: FileChooserParams?
                ): Boolean {
                    mUploadCallbackAboveL = filePathCallback

                    PictureSelector.create(this@FeedbackActivity)
//                            .openGallery(PictureMimeType.ofImage())
                            .openGallery(PictureMimeType.ofAll())
                            .theme(R.style.picture_default_style)
                            .maxSelectNum(9)
                            .minSelectNum(1)
                            .imageSpanCount(4)
                            .selectionMode(PictureConfig.MULTIPLE)
                            .previewImage(true)
                            .isCamera(true)
                            .imageFormat(PictureMimeType.PNG)
                            .isZoomAnim(true)
                            .compress(true)
                            .glideOverride(160, 160)
                            .hideBottomControls(true)
                            .compressSavePath(cacheDir.absolutePath)
                            .isGif(false)
                            .previewEggs(true)
                            .minimumCompressSize(100)
                            .forResult(PictureConfig.CHOOSE_REQUEST)

                    return true
                }
            }
            webViewClient = WebViewClient()
        }

        topBarRelative.setOnClickListener { onBackPressed() }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureConfig.CHOOSE_REQUEST) {
            if (mUploadCallbackAboveL == null) return

            val selectList = PictureSelector.obtainMultipleResult(data) as ArrayList<LocalMedia>
            val results: ArrayList<Uri> = ArrayList()
            selectList.forEach {
                if (PictureMimeType.isPictureType(it.pictureType) == PictureMimeType.ofVideo()) {
                    results.add(Uri.fromFile(File(it.path)))
                }
                else {
                    results.add(Uri.fromFile(File(it.compressPath)))
                }
            }
            mUploadCallbackAboveL?.onReceiveValue(results.toTypedArray())
        }
    }

}
